package org.wxy.sdk.util;

import android.content.Context;

import com.afollestad.materialdialogs.MaterialDialog;

import org.wxy.sdk.R;

import java.util.List;

/**
 * Created by wxy on 2018/05/27.
 *
 * @author wxy
 * @date on 2018/05/27 11:32
 */
public class DialogFactory {
    private static DialogFactory sFactory;
    private DialogFactory(){}

    private MaterialDialog progressDialog;

    public static DialogFactory getInstance() {
        if (sFactory ==  null) {
            synchronized (DialogFactory.class) {
                if (sFactory == null) {
                    sFactory = new DialogFactory();
                }
            }
        }
        return sFactory;
    }

    public MaterialDialog.Builder builder(Context context) {
        return new MaterialDialog.Builder(context);
    }

    public void showProgressDialog(Context context, String message) {
        hideProgressDialog();
        progressDialog = new MaterialDialog.Builder(context)
                .progress(true, 0)
                .content(message)
                .canceledOnTouchOutside(false).build();
        progressDialog.show();
    }

    public void hideProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    public void releaseProgress() {
        hideProgressDialog();
        progressDialog = null;
    }

    public void showSuccessDialog(Context context, String title, String message) {
        new MaterialDialog.Builder(context)
                .title(title)
                .content(message)
                .iconRes(R.mipmap.ic_success)
                .positiveText(R.string.yes)
                .show();
    }

    public void showErrorDialog(Context context, String title, String message) {
        new MaterialDialog.Builder(context)
                .title(title)
                .content(message)
                .iconRes(R.mipmap.ic_error)
                .positiveText(R.string.yes)
                .show();
    }

    public void showWarningDialog(Context context, String message) {
        showWarningDialog(context, message, null, null);
    }

    public void showWarningDialog(Context context, String message, MaterialDialog.SingleButtonCallback positiveCallback, MaterialDialog.SingleButtonCallback negativeCallback) {
        new MaterialDialog.Builder(context)
                .title(R.string.warning)
                .content(message)
                .iconRes(R.mipmap.ic_warning)
                .positiveText(R.string.yes)
                .onPositive(positiveCallback)
                .negativeText(R.string.cancel)
                .onNegative(negativeCallback)
                .show();
    }

    public void showConfirmingDialog(Context context, String message, MaterialDialog.SingleButtonCallback positiveCallback, MaterialDialog.SingleButtonCallback negativeCallback) {
        new MaterialDialog.Builder(context)
                .title(R.string.confirming)
                .content(message)
                .iconRes(R.mipmap.ic_success)
                .positiveText(R.string.yes)
                .onPositive(positiveCallback)
                .negativeText(R.string.cancel)
                .onNegative(negativeCallback)
                .show();
    }

    public MaterialDialog showSingleChoiceListDialog(Context context, String title, List<String> items, MaterialDialog.ListCallbackSingleChoice callbackSingleChoice) {
        return new MaterialDialog.Builder(context)
                .title(title)
                .items(items)
                .itemsCallbackSingleChoice(-1, callbackSingleChoice)
                .positiveText(R.string.yes)
                .negativeText(R.string.cancel)
                .show();
    }

    public void showMultipleChoiceListDialog(Context context, String title, List<String> items, MaterialDialog.ListCallbackMultiChoice callbackMultiChoice) {
        new MaterialDialog.Builder(context)
                .title(title)
                .items(items)
                .itemsCallbackMultiChoice(null, callbackMultiChoice)
                .positiveText(R.string.yes)
                .show();
    }
}
